import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index.js';
import { ElMessage } from 'element-plus';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/headNo' },
    { path: '/login', component: () => import('@/views/Login/LoginPage.vue') },
    { path: '/headNo', component: () => import('@/views/HeadNo/HeadNo.vue') },
    {
      path: '/headYes',
      component: () => import('@/views/HeadYes/HeadYes.vue'),
      children: [
        { path: 'partner/find', component: () => import('@/views/Partner/FindPartner.vue') },
        { path: 'partner/my', component: () => import('@/views/Partner/MyPartner.vue') },
        { path: 'partner/study', component: () => import('@/views/Partner/StudyGroup.vue') },
        { path:'partner/question', component:()=>import('@/views/Partner/Question.vue')}
      ]
    },
    { path: '/user/finish', component: () => import('@/views/User/UserFinish.vue') },
    { path: '/user/UserInformation', component: () => import('@/views/User/UserInformation.vue') },
    { path: '/partner/FindPartnerContent', component: () => import('@/views/Partner/FindPartnerContent.vue') },
    { path:'/partner/FindPartnerInfo',component:()=>import('@/views/Partner/FindPartnerInfo.vue')},
    { path:'/partner/QuestionDetail',component:()=>import('@/views/Partner/QuestionDetail.vue')},

  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const publicPaths = ['/login', '/headNo', '/user/finish'];

  if (publicPaths.includes(to.path)) {
    next();
  } else {
    if (userStore.token) {
      next();
    } else {
      ElMessage.warning('请先登录后再访问该页面');
      next({ path: '/login', query: { redirect: to.fullPath } });
    }
  }
});

export default router
